<template>
	<view class="my-page">


		<!-- 用户信息卡片 -->
		<view class="user-card">
			<view class="user-avatar">
				<u-avatar :src="userInfo.avatarUrl || defaultAvatar" :size="60" mode="circle"></u-avatar>
			</view> 
			<view class="user-info">
				<text class="user-name">{{ userInfo.nickName || '未登录'}}</text>
			</view>
		</view>

		<!-- 用户统计片 -->
		<view class="user-statistics">
			<view class="function-area"><u-icon label="总结" size="40" name="file-text" color="#FFB900"
					labelPos="bottom"></u-icon>
			</view>
			<view class="function-area"><u-icon @click="navigateTo('records')" label="记录" size="40" name="file-text" color="#FFB900"
					labelPos="bottom"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgImage: '/static/images/my_bg.png',
				userId: '',
				isLogin: false,
				defaultAvatar: '/static/image/default_avatar.png'

			}
		},
		computed: {
			// 获取全局用户信息
			userInfo() {
				return this.$store.state.userInfo
			}
		},
		onReady() {
			console.log("load")
			// 从 store 读取用户信息
			const userInfo = this.$store.state.userInfo
			if (!userInfo) {
				// 无信息则跳授权页
				uni.navigateTo({
					url: '/pages/authorize/index'
				})
			}
		},
		methods: {
			// 检查登录状态
			checkLoginStatus() {
				const userInfo = uni.getStorageSync('userInfo');
				if (userInfo && userInfo.nickName) {
					this.userInfo = userInfo;
					this.userId = this.generateUserId();
					this.isLogin = true;
				}
			},

			// 生成用户ID
			generateUserId() {
				const timestamp = new Date().getTime().toString().slice(-6);
				const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
				return `U${timestamp}${random}`;
			},

			// 微信登录
			loginByWechat() {
				uni.getUserProfile({
					desc: '用于完善用户资料',
					success: (res) => {
						this.userInfo = res.userInfo;
						this.userId = this.generateUserId();
						this.isLogin = true;

						// 存储用户信息到本地
						uni.setStorageSync('userInfo', this.userInfo);

						// 登录成功提示
						uni.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 2000
						});
					},
					fail: (err) => {
						console.log('获取用户信息失败', err);
						uni.showToast({
							title: '授权失败，部分功能将受限',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},

			// 退出登录
			logout() {
				uni.showModal({
					title: '确认退出',
					content: '退出后将无法使用个人功能，是否继续？',
					success: (res) => {
						if (res.confirm) {
							// 清除本地存储的用户信息
							uni.removeStorageSync('userInfo');
							this.userInfo = {};
							this.isLogin = false;

							uni.showToast({
								title: '已退出登录',
								icon: 'success',
								duration: 2000
							});
						}
					}
				});
			},

			// 页面跳转
			navigateTo(page) {
				if (this.isLogin) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				let url = '';
				switch (page) {
					case 'assets':
						url = '/pages/assets/index';
						break;
					case 'records':
							url = '/pages/record/index';
							break;
					case 'ranking':
						url = '/pages/ranking/index';
						break;
					case 'news':
						url = '/pages/news/index';
						break;
					case 'settings':
						url = '/pages/settings/index';
						break;
				}

				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-page {
		background-color: #F5F7FA;
		min-height: 100vh;
	}

	.header-bg {
		height: 200rpx;
		overflow: hidden;
		position: relative;
	}

	.bg-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}


	.user-card {
		position: relative;
		display: flex;
		flex-direction: row;
		align-items: center;
		background-color: #FFB900;
		padding: 40rpx 30rpx;
		width: 100%;
		height: 150px;
		margin-bottom: -50rpx;
		z-index: 5;
	}

	.user-statistics {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		background-color: #FFFFFF;
		border-radius: 16rpx;
		margin: 20rpx 30rpx 50rpx 30rpx;
		padding: 40rpx 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
		position: relative;
		z-index: 10;
	}

	.function-area {
		padding: 0 50rpx 0 50rpx;
	}

	.user-avatar {
		display: flex;
		justify-content: center;
		flex: 1;
	}

	.user-info {
		flex: 2;
	}

	.user-name {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
	}

	.user-id {
		font-size: 24rpx;
		color: #999999;
	}

	.logout-btn {
		width: 200rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding: 10rpx 0;
	}


	.login-container {
		padding: 80rpx 0;
		text-align: center;
	}

	.default-avatar {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		margin: 0 auto 40rpx;
		background-color: #E5E9F2;
		padding: 20rpx;
	}

	.login-tip {
		font-size: 28rpx;
		color: #999999;
		display: block;
		margin-bottom: 40rpx;
	}

	.login-btn {
		width: 500rpx;
		margin: 0 auto;
		font-size: 32rpx;
	}


	.function-list {
		margin: 0 30rpx;
	}
</style>